#
# fan.py
#
# The Fan class
#

import pygame
import data

import constants as c
import sounds

FAN_READY = 0
FAN_BLOW = 1
FAN_SUCK = 2

class Fan(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

        self.image_normal, self.rect = data.load_image("fan.png", None)
        self.image_blow, _ = data.load_image("fan_blow.png", None)
        self.image_suck, _ = data.load_image("fan_suck.png", None)

        self.image = self.image_normal
        self.rect.topleft = (350, 560)

        self.state = FAN_READY

    def click(self, button):
        # if we are ready to blow/suck
        if self.state == FAN_READY:
            # add the timer event, controls blow/suck rate
            pygame.time.set_timer(c.EVENT_FAN_TIMEOUT, c.EVENT_FAN_TIMEOUT_TIME)

            # now check to see which button is pressed and change image
            # accordingly
            if button == 1:
                self.state = FAN_BLOW
                self.image = self.image_blow
                sounds.play_sfx('fan_blow.wav')
            elif button == 3:
                self.state = FAN_SUCK
                self.image = self.image_suck
                sounds.play_sfx('fan_suck.wav')

            return True

        else:
            return False

    def timeout(self):
        # once timeout is called we can return to the ready state
        self.state = FAN_READY
        self.image = self.image_normal

    def update(self, time_diff):
        self.rect.centerx = pygame.mouse.get_pos()[0]

